<?php
/**
 * 梯控服务
 */
namespace service;

use think\Cache;
use think\Request;
use think\Db;

class LadderService
{

    const LGSPLIT = "*$@$*";

    //第一个字节代表二维码类型：00是一次性二维码  01是带有效期的二维码
    protected $qr_type = "00";
    //电梯编号
    protected $user_num = "000001";
    //卡类型
    protected $uid_class = "01";
    //电梯号
    protected $eid = "01";
    //小区号
    protected $aid = "01";
    //常量
    protected $num = "00";

    //秘钥
    protected $aes_key = "74637A59754349545A77344C32554E61";


    /**
     * @var object 对象实例
     */
    protected static $instance;

    /**
     * 初始化
     * @access public
     * @param array $options 参数
     * @return \think\Request
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }
        return self::$instance;
    }

    public function getqrcode($floorinfo=[]){
        $app_id = $this->qr_type.$this->user_num;
        $ctime = time();
        //生成二维码时间
        $act_time = sprintf("%08x",$ctime);
        //有效期
        $validate_time = "ffffffff";
        if($this->qr_type == "01"){
            $year_time = intval(date("Y",$ctime) - 2000 + 5);
            $month_time = intval(date("m",$ctime));
            $day_time = intval(date("d",$ctime));
            $hour_time = intval(date("H",$ctime));
            $validate_time = sprintf("%02x",$year_time).sprintf("%02x",$month_time).sprintf("%02x",$day_time).sprintf("%02x",$hour_time);
        }
        //楼层权限
        //$floor_permissions = "ffffffffffffffff";
        //$floor_permissions = "1000000000000000";
        for($i = 4;$i <= 13;$i++){
            $tempvar = "bit".$i;
            $$tempvar = "0";
            if(in_array($i."F",$floorinfo)){
                $$tempvar = "1";
            }
        }
        $hight_4bit = "";
        for ($i = 7; $i >= 4;$i--){
            $tempvar = "bit".$i;
            $hight_4bit .= $$tempvar;
        }
        $byte1 = $hight_4bit."1111";
        $byte1 = sprintf("%x",bindec($byte1));
        //dump($byte1);
        $byte2_low_4bit = "";
        for ($i = 11; $i >= 8;$i--){
            $tempvar = "bit".$i;
            $byte2_low_4bit .= $$tempvar;
        }
        //dump($byte2_low_4bit);exit;
        $byte2_hight_2bit = "";
        for ($i = 13; $i >= 12;$i--){
            $tempvar = "bit".$i;
            $byte2_hight_2bit .= $$tempvar;
        }
        $byte2 = "11".$byte2_hight_2bit.$byte2_low_4bit;
        $byte2 = sprintf("%x",bindec($byte2));
        //dump($byte2);
        $floor_permissions = $byte1.$byte2."000000000000";
        //dump($floor_permissions);
        $hex_data = $app_id.$act_time.$this->uid_class.$this->eid.$this->aid.$this->num.$validate_time.$floor_permissions;
        //dump("明文：");
        //dump($hex_data);
        $base64_string = base64_encode(hex2bin($hex_data));
        //dump("base64：");
        //dump($base64_string);
        //dump("===============================");
        $iv = $key = hex2bin($this->aes_key);
        $encrypted_string = openssl_encrypt($base64_string, 'AES-128-CBC', $key, 0, $iv);
        //dump("密文：");
        //dump($encrypted_string);
        //$decrypted_string = openssl_decrypt($encrypted_string, 'AES-128-CBC', $key, 0, $iv);
        //dump("解密：");
        //dump($decrypted_string);
        //$old_hex_string = bin2hex(base64_decode($decrypted_string));
        //dump("还原：");
        //dump($old_hex_string);exit;
        return ['enceypted'=>$encrypted_string,'hex_data'=>$hex_data];
        //return $encrypted_string;
    }

}